<?php

namespace app\common\business;

use app\common\lib\Str;
use think\facade\Cache;
use think\Exception;

class Exercises
{
    public $exercisesObj = null;
    public $answerObj = null;

    public function __construct()
    {
        $this->exercisesObj = new \app\common\model\Exercises();
        $this->answerObj = new \app\common\model\Answer();
    }

    public function one_exercises($id)
    {
        return $this->exercisesObj->find($id);
    }

    public function exercises_all($data)
    {
        return $this->exercisesObj->where($data)->select();
    }

    public function exercises($data)
    {
        return $this->exercisesObj->exercises($data);
    }

    public function add_exercises($data)
    {
        return  $this->exercisesObj->save($data);
    }

    public function del_exercises($id)
    {
        return $this->exercisesObj::destroy($id);;
    }

    public function edit_exercises($data)
    {
        return $this->exercisesObj::update($data);
    }

    public function edit_answer($where_data,$answer)
    {
        return $this->answerObj->edit_answer($where_data,$answer);
    }

    public function sign_in_exercises($ids){
        $sign_in_date = formatTimestamp(time());
        // 根据主键获取多个数据
        $list = $this->exercisesObj::select($ids);
        // 对数据集进行遍历操作
        foreach($list as $key=>$exercises){
            $sign_in = [];
           if(!empty($exercises->sign_in)){
               $sign_in = json_decode($exercises->sign_in);
           }
           if(!in_array($sign_in_date,$sign_in)){
               $sign_in[] = $sign_in_date;
               $exercises->sign_in = json_encode($sign_in);
               $exercises->save();
           }
        }
        return true;
    }
}